package com.ruoyi.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.material.service.IMateriaService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.material.domain.MateriaType;
import com.ruoyi.material.service.IMateriaTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 材料类型Controller
 * 
 * @author renyuxin
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/material/materialType")
public class MateriaTypeController extends BaseController
{
    @Autowired
    private IMateriaTypeService materiaTypeService;
    @Autowired
    private IMateriaService materiaService;

    /**
     * 查询材料类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:list')")
    @GetMapping("/list")
    public TableDataInfo list(MateriaType materiaType)
    {
        startPage();
        List<MateriaType> list = materiaTypeService.selectMateriaTypeList(materiaType);
        return getDataTable(list);
    }

    /**
     * 导出材料类型列表
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:export')")
    @Log(title = "材料类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MateriaType materiaType)
    {
        List<MateriaType> list = materiaTypeService.selectMateriaTypeList(materiaType);
        ExcelUtil<MateriaType> util = new ExcelUtil<MateriaType>(MateriaType.class);
        util.exportExcel(response, list, "材料类型数据");
    }

    /**
     * 获取材料类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:query')")
    @GetMapping(value = "/{materiatypeId}")
    public AjaxResult getInfo(@PathVariable("materiatypeId") Long materiatypeId)
    {
        return success(materiaTypeService.selectMateriaTypeByMateriatypeId(materiatypeId));
    }

    /**
     * 新增材料类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:add')")
    @Log(title = "材料类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MateriaType materiaType)
    {
        if(materiaTypeService.checkNameUnique(materiaType)){
            return error("材料类型名称重复,请换一个!");
        }
        return toAjax(materiaTypeService.insertMateriaType(materiaType));
    }

    /**
     * 修改材料类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:edit')")
    @Log(title = "材料类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MateriaType materiaType)
    {
        if(materiaTypeService.checkNameUnique(materiaType)){
            return error("材料类型名称重复,请换一个!");
        }
        return toAjax(materiaTypeService.updateMateriaType(materiaType));
    }

    /**
     * 删除材料类型
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:remove')")
    @Log(title = "材料类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{materiatypeIds}")
    public AjaxResult remove(@PathVariable Long[] materiatypeIds)
    {
        //有子节点则不允许删除
        if(materiaTypeService.hasChildren(materiatypeIds[0])){
            return error("先删除子节点，再删除本节点");
        }

        return materiaTypeService.deleteMateriaTypeByMateriatypeIds(materiatypeIds);
    }
    /**
     * 材料类型 树
     */
    @PreAuthorize("@ss.hasPermi('material:materialType:query')")
	@GetMapping("/getTree")
    public AjaxResult getTree()
    {
        MateriaType root = materiaTypeService.getTree();
        return success(root);
    }

}
